<?php
class connect
{
	private $server = 'localhost';
	private $db = 'thoitrang';
	private $user = 'root';
	private $pw = 'usbw';
	public $conn;
	public $result;
	//============================================
	//goi ham tao ket noi database 
	function __construct()
	{
		$this->connect_db();
	}
	
	//============================================
	//goi ham huy database
	function __destruct()
	{
		$this->deconnect_db();
	}
	
	//==============================================
	//ham ket noi database
	function connect_db()
	{
		//tao bien connect
		$this->conn = mysql_pconnect($this->server,$this->user,$this->pw) or die ('no connect');//ket noi database
		if($this->conn)
			mysql_select_db($this->db);//chon database ket noi
	}
	
	//=============================================
	//ham dong ket noi 
	function deconnect_db()
	{
		if($this->conn) 
		{
			mysql_close($this->conn);
		}
	}
	
	//==============================================
	//thuc thi 1 cau lenh truy van 
	function query_db($sql)
	{
		$this->result = mysql_query($sql,$this->conn);
		/*if(!$this->result){
			die (mysql_error($this->conn)); //Bat loi tu mysql trigger
		}*/
		mysql_query('set names "UTF8"');
	}
	//=============================================
	function totalRow()
	{
		return mysql_num_rows($this->result);
	}
	//==============================================
	//lay mot hang du lieu trong bang
	function fetchOne()
	{
		return mysql_fetch_assoc($this->result);
	}
	
	//==============================================
	//lay tat cac dong du lieu trong bang
	function fetchAll()
	{
		while ($row = $this->fetchOne())
		{
			$data[] = $row; //neu hang ton tai thi bo vao data tao thanh mang 2 chieu
		}
		return $data;
	}
}
?>